package com.mixpush.flutter_mix_push;

import android.content.Context;
import android.os.Handler;
import android.util.Log;

import com.mixpush.core.MixPushMessage;
import com.mixpush.core.MixPushPlatform;
import com.mixpush.core.MixPushReceiver;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import io.flutter.plugin.common.MethodChannel;

public class MyPushReceiver extends MixPushReceiver {
    private MethodChannel _channel;
    private Handler _handler;

    MyPushReceiver(MethodChannel channel, Handler handler) {
        this._channel = channel;
        this._handler = handler;
    }

    @Override
    public void onRegisterSucceed(Context context, MixPushPlatform mixPushPlatform) {
        // 这里需要实现上传regId和推送平台信息到服务端保存，
        //也可以通过MixPushClient.getInstance().getRegisterId的方式实现
        JSONObject json = new JSONObject();
        try {
            json.put("regId", mixPushPlatform.getRegId());
            json.put("platformName", mixPushPlatform.getPlatformName());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        Log.e("onRegisterSucceed", "$mixPushPlatform");
        _handler.post(new Runnable() {
            @Override
            public void run() {
                _channel.invokeMethod("onRegisterSucceed", json.toString());
            }
        });
    }

    @Override
    public void onNotificationMessageClicked(Context context, MixPushMessage message) {
        // 通知栏消息点击触发，实现打开具体页面，打开浏览器等。
        JSONObject json = new JSONObject();
        try {
            json.put("title", message.getTitle());
            json.put("description", message.getDescription());
            json.put("platform", message.getPlatform());
            json.put("payload", message.getPayload());
            json.put("passThrough", message.isPassThrough() ? "1" : "0");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        Log.e("onNotifyMsgClicked", "$message");
        _handler.post(new Runnable() {
            @Override
            public void run() {
                _channel.invokeMethod("onNotifyMsgClicked", json.toString());
            }
        });
    }
}
